/**
 * Created by tsy on 2016/10/27.
 */

function submitSearch() {
    $('#default_table').datagrid('load',{
        select_borrowNum: $('#select_borrowNum').val(),
        select_custName:$('#select_custName').val(),
        select_facName:$("#select_facName").val(),
        select_delayStatus:$("#select_delayStatus").val(),
        select_borrowStatus:$("#select_borrowStatus").combobox('getValue'),
        select_start_time:$("#select_start_time").datebox('getValue'),
        select_end_time:$("#select_end_time").datebox('getValue')
    });
}
function status_table(i){
    $("#select_borrowStatus").val(i);
    $("#paymoney").css("display","none");
    if(i==1){
        $("#paymoney").css("display","inline");
        var title="等待放款";
        var columns=[[
            {field: 'borrow_id', title: '分期编号', width: '9%'},
            {field: 'name', title: '商户类型', width: '7%'},
            {field: 'fac_simple_name', title: '商户简称', width: '8%'},
            {field: 'borrow_money', title: '分期金额', width: '9%'},
            {field: 'borrow_rate', title: '利率', width: '7%'},
            {field: 'borrow_loanperiod', title: '分期期限', width: '7%'},
            {field: 'cust_name', title: '客户姓名', width: '9%'},
            {field: 'borrow_passtime', title: '审核通过时间', width: '10%'}
        ]]
    }else if(i==4){
        var title="已完成";
        var columns=[[
            {field: 'borrow_id', title: '分期编号', width: '9%'},
            {field: 'name', title: '商户类型', width: '7%'},
            {field: 'fac_simple_name', title: '商户简称', width: '8%'},
            {field: 'borrow_money', title: '分期金额', width: '9%'},
            {field: 'borrow_rate', title: '利率', width: '7%'},
            {field: 'borrow_loanperiod', title: '分期期限', width: '7%'},
            {field: 'cust_name', title: '客户姓名', width: '9%'},
            {field: 'binfo_opendate', title: '放款时间', width: '10%'},
            {field: 'buinfo_repaydown', title: '订单还清时间', width: '10%'},
            {field: 'borrow_invest', title: '投资打包', width: '10%'}
        ]]
    }else if(i==5){
        var title="已取消";
        var columns=[[
            {field: 'borrow_id', title: '分期编号', width: '9%'},
            {field: 'name', title: '商户类型', width: '7%'},
            {field: 'fac_simple_name', title: '商户简称', width: '8%'},
            {field: 'borrow_money', title: '分期金额', width: '9%'},
            {field: 'borrow_rate', title: '利率', width: '7%'},
            {field: 'borrow_loanperiod', title: '分期期限', width: '7%'},
            {field: 'cust_name', title: '客户姓名', width: '9%'},
            {field: 'binfo_opendate', title: '放款时间', width: '10%'}
        ]]
    }else if(i==10){
        var title="进行中和已完成";
        var columns=[[
            {field: 'borrow_id', title: '分期编号', width: '9%'},
            {field: 'name', title: '商户类型', width: '7%'},
            {field: 'fac_simple_name', title: '商户简称', width: '8%'},
            {field: 'borrow_money', title: '分期金额', width: '9%'},
            {field: 'borrow_rate', title: '利率', width: '7%'},
            {field: 'borrow_loanperiod', title: '分期期限', width: '7%'},
            {field: 'cust_name', title: '客户姓名', width: '9%'},
            {field: 'binfo_opendate', title: '放款时间', width: '10%'},
            {field: 'bsta_name', title: '借款状态', width: '10%'}
        ]]
    }else{
        var title="进行中";
        var columns=[[
            {field: 'borrow_id', title: '分期编号', width: '9%'},
            {field: 'name', title: '商户类型', width: '7%'},
            {field: 'fac_simple_name', title: '商户简称', width: '8%'},
            {field: 'borrow_money', title: '分期金额', width: '9%'},
            {field: 'borrow_rate', title: '利率', width: '7%'},
            {field: 'borrow_loanperiod', title: '分期期限', width: '7%'},
            {field: 'cust_name', title: '客户姓名', width: '9%'},
            {field: 'binfo_opendate', title: '放款时间', width: '7%'},
            {field: 'end_date', title: '下期还款', width: '8%'},
            {field: 'borrow_invest', title: '投资打包', width: '9%'}
        ]]
    }
    $('#default_table').datagrid({
        title:title,
        queryParams:{
            select_borrowStatus: i
        },
        columns:columns
    });
}

function resetSearch()
{
    $("#select_borrowNum").textbox("setValue","");
    $("#select_custName").textbox("setValue","");
    $("#select_checkState").combobox("setValue","");
    $("#select_time").textbox("setValue","");
}

//function formatAction(value,row,index)
//{
//    var a = '';
//    var b = '';
//    if (row.fac_simple_name != '总本金:' && row.fac_simple_name != '存量本金:')
//    {
//        if (row.check_payed == '未放款')
//        {
//            var a = '<input type="button" value="放款" class="input" onclick="paymoney('+row.borrow_id+');">';
//        }
//        var b = '<input type="button" value="详情" class="input" onclick="borrowbusiness_detail('+row.borrow_id+');">';
//    }
//
//    return a+'&nbsp;&nbsp;'+b;
//}

function borrowbusiness_detail() {
    var obj=$("#default_table").datagrid('getSelected');
    //console.log($('#customer_table').datagrid('getPager').data("pagination").options.pageNumber);
    if(obj==null){
        alert("请选择借款订单！");
        return;
    }
    var borrow_id=obj.borrow_id;
    window.parent.addTab('借款业务详情','../../borrow/borrowbusiness/borrowbusiness_detail?borrow_id='+borrow_id);
}


//	放款操作
function paymoney()
{
    var obj=$("#default_table").datagrid('getSelected');
    //console.log($('#customer_table').datagrid('getPager').data("pagination").options.pageNumber);
    if(obj==null){
        alert("请选择借款订单！");
        return;
    }
    var borrow_id=obj.borrow_id;
    var binfo_state=obj.binfo_state;
    if(binfo_state!=1){
        alert("该订单不能放款！");
        return;
    }
    var flag = confirm("是否确定放款");
    if(flag)
    {
        var data = {"borrow_id":borrow_id};
        var url="../../borrow/Borrowbusiness/borrowbusiness_list_paymoney";
        $.ajax({
            type: "post",
            async: true,
            data: data,
            url: url,
            success: function(d)
            {
                var dataObj = JSON.parse(d);
                var state = dataObj.state;
                var mes = dataObj.message;
                if(state == 1)
                {
                    alert(mes);
                    window.location.reload();
                }
                else
                {
                    alert(mes);
                    window.location.reload();
                }
            },
            error: function(e)
            {
                alert("网络错误，请联系程序员！");
            }
        });
    }
}


